﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using LprMiddleWare.HangWang;
using MessageBox = System.Windows.MessageBox;

namespace LprMiddleWare.Views
{
    /// <summary>
    /// Description for MainView.
    /// </summary>
    public partial class ShellWindow : Window
    {

        private NotifyIcon _notifyIcon;
        private WindowState _oldWindowState;
        private DispatcherTimer _timer;
        private HanWangController controller;
        private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();

        /// <summary>
        /// Initializes a new instance of the MainView class.
        /// </summary>
        public ShellWindow()
        {

            this.Closed += new EventHandler(ShellWindow_Closed);
            this.Closing += new System.ComponentModel.CancelEventHandler(ShellWindow_Closing);



            InitializeComponent();

            this.Loaded += new RoutedEventHandler(ShellWindow_Loaded);

            _notifyIcon = new NotifyIcon();
            _notifyIcon.Icon = Properties.Resources.CarRepairBlue2;
            _notifyIcon.BalloonTipTitle = "车牌识别中间件";
            _notifyIcon.BalloonTipText = "正在后台运行，单击此处恢复窗口";
            _notifyIcon.Text = "车牌识别中间件";
            _notifyIcon.Click += new EventHandler(_notifyIcon_Click);
        }

        void ShellWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //var res = MessageBox.Show("退出系统吗？", this.Title, MessageBoxButton.YesNo);
            //System.Diagnostics.Debug.WriteLine(sender.ToString());
            //if (res == MessageBoxResult.No)
            //{
            //    e.Cancel = true;

            //}

            _logger.Info("退出程序");
        }

        void ShellWindow_Closed(object sender, EventArgs e)
        {
            App.Current.Shutdown();
        }

        void ShellWindow_Loaded(object sender, RoutedEventArgs e)
        {


            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Start();
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            this.WindowState = WindowState.Minimized;
            _timer.Stop();
        }

        void _notifyIcon_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = _oldWindowState;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.MainFrame.Navigate(new Uri(@"Views/SettingPage.xaml", UriKind.Relative));
            //controller = new HangWang.HanWangController();
            //controller.StatusReport += new EventHandler<HangWang.ControlEventArgs>(controller_StatusReport);
            //controller.Connect("192.168.1.251");

        }

        void controller_StatusReport(object sender, HangWang.ControlEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.EventArgs.type.ToString() + " " + e.EventArgs.szMsg);
            if (e.EventArgs.type == tagHWCtrlEventType.HWCTRL_EVENT_ESTABLISH)
            {
                var c = sender as HanWangController;

                var oldtime = c.ReadTime();
                System.Diagnostics.Debug.WriteLine("original time: " + oldtime);

                var result = c.SyncTime(oldtime.AddHours(2));
                System.Diagnostics.Debug.WriteLine(result);
                oldtime = c.ReadTime();
                System.Diagnostics.Debug.WriteLine("add 2 hours: " + oldtime);
                c.Dispose();
            }
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Hide();
                if (_notifyIcon != null)
                {
                    _notifyIcon.ShowBalloonTip(2000);
                }
            }
            else
            {
                _oldWindowState = this.WindowState;
            }
        }

        private void CheckTrayIcon()
        {
            ShowTrayIcon(!this.IsVisible);
        }

        private void ShowTrayIcon(bool show)
        {
            if (_notifyIcon != null)
            {
                _notifyIcon.Visible = show;
            }
        }

        private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            CheckTrayIcon();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (_notifyIcon != null)
            {
                _notifyIcon.Visible = false;
                _notifyIcon = null;
            }
        }

        private void mainViewButton_Click(object sender, RoutedEventArgs e)
        {
            this.MainFrame.Navigate(new Uri(@"Views/MainPage.xaml", UriKind.Relative));
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var about = new WPFAboutBox(this);
            about.ShowDialog();
        }


        private void SettingsMenu_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new Views.SettingsWindow();
            dlg.ShowDialog();
        }
    }
}